package com.useeinfo.asg.open.admin;

import com.useeinfo.asg.common.utils.CommUtil;
import com.useeinfo.asg.modules.logging.biz.ArticleBiz;
import com.useeinfo.asg.modules.logging.entity.Address;
import com.useeinfo.asg.modules.logging.entity.Article;
import com.useeinfo.asg.modules.security.biz.SystemUserBiz;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.xiaoleilu.hutool.util.CollectionUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by THINK on 2017/4/27.
 */
@Controller
@RequestMapping("/admin/article")
public class ArticleAdminManageAction {

    @Autowired
    private ArticleBiz articleBiz;
    @Autowired
    private SystemUserBiz systemUserBiz;

    @RequestMapping("/list")
    public String list(HttpServletRequest request, ModelMap mv) {

        return "/admin/article/list";
    }

    @RequestMapping("/listJson")
    @ResponseBody
    public JSONObject listJosn(@RequestParam(value = "page", required = false) String pageNowParam,
                               @RequestParam(value = "rows", required = false) String pageSizeParam,
                               @RequestParam(value = "name", required = false) String sidxParam) {

        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);
        queryParam.getSqlMap().put("systemUserID",systemUserBiz.getLoginUser().getId().toString());
        return articleBiz.findJSONList(queryParam);
    }

    @RequestMapping("/addOredit")
    @ResponseBody
    public JSONObject add(HttpServletRequest request,String id,String articleName,String simple,String content) {
        Article article = articleBiz.findModel(CommUtil.null2Long(id));
        if(null==article){
            article = new Article();
            article.setCreateTime(new Date());
        }else {
            article.setUpdateTime(new Date());
        }
        article.setArticleName(CommUtil.null2String(articleName));
        article.setContent(CommUtil.null2String(content));
        article.setSimple(CommUtil.null2String(simple));
        article.setSystemUser(systemUserBiz.getLoginUser());
        articleBiz.addOrUpdate(article);
       return new JSONObject();
    }



    @RequestMapping(value = "/editPage", produces = "text/html;charset=UTF-8")
    public String edit(HttpServletRequest request, ModelMap mv) {


        String id = request.getParameter("id");
        Article article = articleBiz.findModel(CommUtil.null2Long(id));
        mv.put("article", article);
        return "/admin/article/edit";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public int delete(HttpServletRequest request, ModelMap mv) {

        String id = request.getParameter("id");
        Article article = articleBiz.findModel(CommUtil.null2Long(id));
        if(null!=article)
            article.setDeleteStatus(true);
        return articleBiz.update(article);
    }
}
